cssimage: Improve new_parse() to select right image type
authorBenjamin Otte <otte@redhat.com>
Tue, 20 Dec 2011 07:52:54 +0000 (08:52 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 9 Jan 2012 17:37:56 +0000 (18:37 +0100)
gtk/gtkcssimage.c

index e46706cbf2a4039f67aa9d72087a4e53f8e8d1b6..968ab7df3bc3e88ea4d5d9b3a42be90adf1855a9 100644 (file)
@@ -162,21 +162,38 @@ GtkCssImage *
 _gtk_css_image_new_parse (GtkCssParser *parser,
                           GFile        *base)
 {
-  GtkCssImage *image;
-  GtkCssImageClass *klass;
+  static const struct {
+    const char *prefix;
+    GType (* type_func) (void);
+  } image_types[] = {
+    { "url", _gtk_css_image_url_get_type }
+  };
+  guint i;
 
   g_return_val_if_fail (parser != NULL, NULL);
   g_return_val_if_fail (G_IS_FILE (base), NULL);
 
-  image = g_object_new (GTK_TYPE_CSS_IMAGE_URL, NULL);
-
-  klass = GTK_CSS_IMAGE_GET_CLASS (image);
-  if (!klass->parse (image, parser, base))
+  for (i = 0; i < G_N_ELEMENTS (image_types); i++)
     {
-      g_object_unref (image);
-      return NULL;
+      if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
+        {
+          GtkCssImage *image;
+          GtkCssImageClass *klass;
+
+          image = g_object_new (image_types[i].type_func (), NULL);
+
+          klass = GTK_CSS_IMAGE_GET_CLASS (image);
+          if (!klass->parse (image, parser, base))
+            {
+              g_object_unref (image);
+              return NULL;
+            }
+
+          return image;
+        }
     }
 
-  return image;
+  _gtk_css_parser_error (parser, "Not a valid image");
+  return NULL;
 }